<?php
namespace Admin\Controller;
use Admin\Model\TagModel;
use Think\Page;
use Admin\Model\FileModel;
use Admin\Model\ProjectModel;
use Org\Util\SFile;

/**
 * 产品列表操作
 *
 * @author xiaoyutab<xiaoyutab@qq.com>
 * @version v1.0.0
 * @copyright (c) 2017, xiaoyutab
 * @adtime 2018-6-1 17:39:28
 */
class ProductController extends BashController{
    /**
     * 分类列表
     * @return object
     */
    public function type_listAction(){
        $model = new TagModel();
        $where = array();
        foreach(explode(' ', I('get.keywords')) as $v){
            $where[] = '%'.$v.'%';
        }
        $where = array(
            'name' => array('like',$where),
        );
        $where['type'] = get_config_id('TagType', '分类');
        $count = $model->where($where)->getCount();
        $page = new Page($count, 10);
        $list = $model->where($where)->getList($page->firstRow, $page->listRows);
        return $this->assign(array(
            'count' => $count,
            'list' => $list,
            'page' => $page->show(),
        ))->display();
    }
    
    /**
     * 添加分类操作
     * @return object
     */
    public function type_addAction(){
        $get = I('get.');
        if(!I('post.')){
            return $this->display();
        }
        $post = I('post.');
        $post['type'] = get_config_id('TagType','分类');
        $tag = new TagModel();
        $tag->create_info($post);
//        if($tag->create_info($post)){
        return $this->success('添加成功',U('type_list'));
//        }
//        return $this->error('系统错误');
    }
    
    /**
     * 修改分类操作
     * @return object
     */
    public function type_saveAction(){
        $get = I('get.');
        $tag = new TagModel();
        $info = $tag->get_info($get['id']);
        if(!I('post.')){
            $this->assign('info',$info);
            return $this->display();
        }
        $post = I('post.');
        $post['type'] = get_config_id('TagType','分类');
        $tag->where(array('id'=>$get['id']))->save($post);
//        if($tag->where(array('id'=>$get['id']))->save($post)){
        return $this->success('修改成功',U('type_list'));
//        }
//        return $this->error('系统错误');
    }
    
    /**
     * 删除数据操作
     * @return object
     */
    public function type_delAction(){
        if(IS_AJAX){
            $id = I('get.id');
            $tag = new TagModel();
            $tag->setDelete($id);
//            if($tag->setDelete($id)){
            return $this->success('删除成功',U('type_list'));
//            }
//            return $this->error('系统错误');
        }
        return $this->error('系统错误',U('Index/index'));
    }
    
    /**
     * 产品列表
     * @return object
     */
    public function product_listAction(){
        $model = new ProjectModel();
        $where = array();
        foreach(explode(' ', I('get.keywords')) as $v){
            $where[] = '%'.$v.'%';
        }
        $where = array(
            'name' => array('like',$where),
        );
        $where['type'] = get_config_id('TagType', '分类');
        $count = $model->where($where)->getCount();
        $page = new Page($count, 10);
        $list = $model->where($where)->getList($page->firstRow, $page->listRows);
        return $this->assign(array(
            'count' => $count,
            'list' => $list,
            'page' => $page->show(),
        ))->display();
    }
    
    /**
     * 添加产品
     * @return type
     */
    public function product_addAction(){
        if(!I('get.id')){
            // 如果是新建产品则直接创建一个已删除的产品进行添加
            $model = new ProjectModel();
            $id = $model->create_info(array('status'=>98));
            if($id){
                (new \Common\Model\LogModel())->create_oth_log($id, 'project');
            }
            $this->redirect('Product/product_add',array('id'=>$id));
        }
        $this->wget('bootstrap-select2');
        $model = new ProjectModel();
        $file = new FileModel();
        $tag = new TagModel();
        if(I('post.')){
            // 如果是提交的数据
            // 先进行图片提交
            $sfile = new SFile();
            $sfile->rm_dirs(RUNTIME_PATH);
            $file->submit_back(I('get.id'), 'project');
            $tag->set_delete_submit('project',I('get.id'));
            $file_content = $this->Upload('file');
            foreach($file_content as $v){
                $id = $file->check_info($v);
                $file->get_link()->create_info(array(
                    'target_name' => $file->get_table_id('project'),
                    'file_id' => $id,
                    'target_id' => I('get.id'),
                ));
            }
            $post = I('post.');
            $post['status'] = 99;
            $model->where(array('id'=>I('get.id')))->save($post);
            return $this->success('保存成功',U('Product/product_list'));
        }
        $file->call_back(I('get.id'), 'project');// 回滚文件连接
        $tag->set_delete_reback('project', I('get.id'));// 回滚标签连接【包含类型和品牌】
        $file_link = $file->get_img_all('project', I('get.id'));
        $info = $model->get_info(I('get.id'));
        $info['file'] = array();
        foreach($file_link as $v){
            $info['file'][] = $v['file_content']['id'];
        }
        $info['file'] = implode(',', $info['file']);
        $this->assign('info',$info);
        $this->assign('file_link',$file_link);
        $tar_list_type = $tag->get_link()->where(array(
                'target_id'=> I('get.id'),
                'target_name'=>$tag->get_table_id('project')
            ))->getList(0,99);
        $tar_list_type_1 = array();
        $tar_list_type_2 = array();
        foreach($tar_list_type as $k => $v){
            $v['tag_info'] = $tag->get_info($v['file_id']);
            if($v['tag_info']['type'] == get_config_id('TagType','分类')){
                $tar_list_type_1[] = $v;
            } else if($v['tag_info']['type'] == get_config_id('TagType','品牌')){
                $tar_list_type_2[] = $v;
            }
        }
        $this->assign('tar_list_type',$tar_list_type_1);
        $this->assign('tar_list_pinpai',$tar_list_type_2);
        $this->assign('tag_list',$tag->where(array('type'=> get_config_id('TagType', '分类')))->getList(0,99));
        $this->assign('tag_pinpai_list',$tag->where(array('type'=> get_config_id('TagType', '品牌')))->getList(0,99));
        // 先将文件编号布置在页面上
        // 如果用户上传文件后将新上传的文件ID放置在页面上
        // 如果用户点击删除了使用ajax先标记一个状态,待提交后再行删除
        // 此处标记的是文件链接的编号
        // 如果用户即删除了照片又添加了新的照片,则一旦提交后则直接删除已删除的照片,然后添加上新的照片提交
        return $this->display();
    }
    
    /**
     * 删除产品
     * @return type
     */
    public function project_delAction(){
        if(IS_AJAX){
            $id = I('get.id');
            $tag = new ProjectModel();
            $tag->setDelete($id);
//            if($tag->setDelete($id)){
            return $this->success('删除成功',U('product_list'));
//            }
//            return $this->error('系统错误');
        }
        return $this->error('系统错误',U('Index/index'));
    }
    
    /**
     * 修改产品信息
     * @return type
     */
    public function project_saveAction(){
        $this->wget('bootstrap-select2');
        $model = new ProjectModel();
        $file = new FileModel();
        $tag = new TagModel();
        if(I('post.')){
            // 如果是提交的数据
            // 先进行图片提交
            $sfile = new SFile();
            $sfile->rm_dirs(RUNTIME_PATH);
            $file->submit_back(I('get.id'), 'project');
            $tag->set_delete_submit('project',I('get.id'));
            $file_content = $this->Upload('file');
            foreach($file_content as $v){
                $id = $file->check_info($v);
                $file->get_link()->create_info(array(
                    'target_name' => $file->get_table_id('project'),
                    'file_id' => $id,
                    'target_id' => I('get.id'),
                ));
            }
            $post = I('post.');
            $post['status'] = 99;
            $model->where(array('id'=>I('get.id')))->save($post);
            return $this->success('保存成功',U('Product/product_list'));
        }
        $file->call_back(I('get.id'), 'project');// 回滚文件连接
        $tag->set_delete_reback('project', I('get.id'));// 回滚标签连接【包含类型和品牌】
        $file_link = $file->get_img_all('project', I('get.id'));
        $info = $model->get_info(I('get.id'));
        $info['file'] = array();
        foreach($file_link as $v){
            $info['file'][] = $v['file_content']['id'];
        }
        $info['file'] = implode(',', $info['file']);
        $this->assign('info',$info);
        $this->assign('file_link',$file_link);
        $tar_list_type = $tag->get_link()->where(array(
                'target_id'=> I('get.id'),
                'target_name'=>$tag->get_table_id('project')
            ))->getList(0,99);
        $tar_list_type_1 = array();
        $tar_list_type_2 = array();
        foreach($tar_list_type as $k => $v){
            $v['tag_info'] = $tag->get_info($v['file_id']);
            if($v['tag_info']['type'] == get_config_id('TagType','分类')){
                $tar_list_type_1[] = $v;
            } else if($v['tag_info']['type'] == get_config_id('TagType','品牌')){
                $tar_list_type_2[] = $v;
            }
        }
        $this->assign('tar_list_type',$tar_list_type_1);
        $this->assign('tar_list_pinpai',$tar_list_type_2);
        $this->assign('tag_list',$tag->where(array('type'=> get_config_id('TagType', '分类')))->getList(0,99));
        $this->assign('tag_pinpai_list',$tag->where(array('type'=> get_config_id('TagType', '品牌')))->getList(0,99));
        // 先将文件编号布置在页面上
        // 如果用户上传文件后将新上传的文件ID放置在页面上
        // 如果用户点击删除了使用ajax先标记一个状态,待提交后再行删除
        // 此处标记的是文件链接的编号
        // 如果用户即删除了照片又添加了新的照片,则一旦提交后则直接删除已删除的照片,然后添加上新的照片提交
        return $this->display();
    }
    
    /**
     * 品牌列表
     * @return type
     */
    public function brand_addAction(){
        $tag = new TagModel();
        $this->wget('bootstrap-select2');
        if(!I('get.id')){
            // 如果是新建品牌则直接创建一个已删除的品牌进行添加
            $id = $tag->create_info(array(
                'status'=>98,
                'type' => get_config_id('TagType','品牌'),
            ));
            if($id){
                (new \Common\Model\LogModel())->create_oth_log($id, 'tag');
            }
            $this->redirect('Product/brand_add',array('id'=>$id));
        }
        $info = $tag->get_info(I('get.id'));
        if(empty($info)){
            $this->error('参数错误');
        }
        $file = new FileModel();
        $project = new ProjectModel();
        if(!I('post.')){
            $file->call_back(I('get.id'), 'tag');// 回滚删除的图片
            $tag->set_delete_reback('project',I('get.id'),true);// 回滚删除的产品
            // 因为删除的产品是产品和标签关联，所以此处删除的表明为project
            // 获取本标签下关联的产品
            $project_list = $tag->get_link()->where(array(
                'file_id' => I('get.id'),
                'target_name' => $tag->get_table_id('project'),
                'status' => 99,
            ))->getList(0,99);
            foreach($project_list as $k => $v){
                $project_list[$k]['project_info'] = $project->get_info($v['target_id']);
            }
            $file_link = $file->get_link()->where(array(
                'target_id' => I('get.id'),
                'target_name' => $tag->get_table_id('tag'),
            ))->getList(0,99);
            foreach($file_link as $k => $v){
                $file_link[$k]['file_content'] = $file->get_info($v['file_id']);
            }
            $this->assign('info',$info);
            $this->assign('file_link',$file_link);
            $this->assign('project_list',$project_list);
            $this->assign('project_list_all',$project->getList(0,99));
            $this->assign('_new',true);
            return $this->display();
        }
        // 如果是提交的数据
        // 先进行图片提交
        $sfile = new SFile();
        $sfile->rm_dirs(RUNTIME_PATH);
        $file->submit_back(I('get.id'), 'tag');
        $tag->set_delete_submit('project',I('get.id'));
        $file_content = $this->Upload('file');
        foreach($file_content as $v){
            $id = $file->check_info($v);
            $file->get_link()->create_info(array(
                'target_name' => $file->get_table_id('tag'),
                'file_id' => $id,
                'target_id' => I('get.id'),
            ));
        }
        $post = I('post.');
        $post['status'] = 99;
        $tag->where(array('id'=>I('get.id')))->save($post);
        return $this->success('保存成功',U('Product/brand_list'));
    }
    
    /**
     * 品牌列表信息
     * @return type
     */
    public function brand_listAction(){
        $tag = new TagModel();
        $where = array();
        foreach(explode(' ', I('get.keywords')) as $v){
            $where[] = '%'.$v.'%';
        }
        $where = array(
            'name' => array('like',$where),
        );
        $where['type'] = get_config_id('TagType', '品牌');
        $count = $tag->where($where)->getCount();
        $page = new Page($count, 10);
        $list = $tag->where($where)->getList($page->firstRow, $page->listRows);
        $file = new FileModel();
        foreach($list as $k => $v){
            $list[$k]['img_link'] = $file->get_img_all('tag',$v['id']);
        }
        return $this->assign(array(
            'count' => $count,
            'list' => $list,
            'page' => $page->show(),
        ))->display();
    }
    
    /**
     * 修改品牌信息
     * @return type
     */
    public function brand_saveAction(){
        $tag = new TagModel();
        $this->wget('bootstrap-select2');
        $info = $tag->get_info(I('get.id'));
        if(empty($info)){
            $this->error('参数错误');
        }
        $file = new FileModel();
        $project = new ProjectModel();
        if(!I('post.')){
            $file->call_back(I('get.id'), 'tag');// 回滚删除的图片
            $tag->set_delete_reback('project',I('get.id'),true);// 回滚删除的产品
            // 因为删除的产品是产品和标签关联，所以此处删除的表明为project
            // 获取本标签下关联的产品
            $project_list = $tag->get_link()->where(array(
                'file_id' => I('get.id'),
                'target_name' => $tag->get_table_id('project'),
                'status' => 99,
            ))->getList(0,99);
            foreach($project_list as $k => $v){
                $project_list[$k]['project_info'] = $project->get_info($v['target_id']);
            }
            $file_link = $file->get_link()->where(array(
                'target_id' => I('get.id'),
                'target_name' => $tag->get_table_id('tag'),
            ))->getList(0,99);
            foreach($file_link as $k => $v){
                $file_link[$k]['file_content'] = $file->get_info($v['file_id']);
            }
            $this->assign('info',$info);
            $this->assign('file_link',$file_link);
            $this->assign('project_list',$project_list);
            $this->assign('project_list_all',$project->getList(0,99));
            $this->assign('_new',true);
            return $this->display('brand_add');
        }
        // 如果是提交的数据
        // 先进行图片提交
        $sfile = new SFile();
        $sfile->rm_dirs(RUNTIME_PATH);
        $file->submit_back(I('get.id'), 'tag');
        $tag->set_delete_submit('project',I('get.id'));
        $file_content = $this->Upload('file');
        foreach($file_content as $v){
            $id = $file->check_info($v);
            $file->get_link()->create_info(array(
                'target_name' => $file->get_table_id('tag'),
                'file_id' => $id,
                'target_id' => I('get.id'),
            ));
        }
        $post = I('post.');
        $post['status'] = 99;
        $tag->where(array('id'=>I('get.id')))->save($post);
        return $this->success('保存成功',U('Product/brand_list'));
    }
    
    /**
     * 删除品牌
     * @return type
     */
    public function brand_delAction(){
        if(IS_AJAX){
            $id = I('get.id');
            $tag = new TagModel();
            $tag->setDelete($id);
//            if($tag->setDelete($id)){
            return $this->success('删除成功',U('brand_list'));
//            }
//            return $this->error('系统错误');
        }
        return $this->error('系统错误',U('Index/index'));
    }
}